package demo.logger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;

/**
 * 文件日志处理
 * @author small瑞
 */
public class LoggerDemo02 {
    public static void main(String[] args) throws IOException {
        Logger logger = Logger.getLogger(LoggerDemo02.class.getName());

        String path = "./log/java_system%g.log";

        Handler handler = new FileHandler(path,true);

        String pattern = "YYYY-MM-dd HH:mm:ss.sss";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

        //Date date = new Date(System.currentTimeMillis());

        //System.out.println(simpleDateFormat.format(date));

        handler.setFormatter(new Formatter() {
            @Override
            public String format(LogRecord record) {
                StringBuilder sb = new StringBuilder();
                sb.append(simpleDateFormat.format(Date.from(record.getInstant())))
                        .append(" ")
                        .append(record.getLoggerName())
                        .append(" ")
                        .append(record.getLevel())
                        .append(" ")
                        .append(record.getMessage())
                        .append("\n");
                return sb.toString();
            }
        });
        logger.setLevel(Level.ALL);
        logger.addHandler(handler);
        logger.info("消息");

        logger.warning("警告");

        logger.config("config");

        logger.severe("severe");

        logger.fine("fine");

        logger.finer("finer");

        handler.close();
    }
}
